package com.cskaoyan.service;

import com.cskaoyan.bean.config.*;
import com.cskaoyan.mapper.ConfigMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.List;

@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    ConfigMapper configMapper;

    @Override
    public List<MallInfoData> mall() {
        List<MallInfoData> mallInfoData = configMapper.getMallInfo("mall");
        return mallInfoData;
    }

    @Override
    public int updateMall(MallParam param){
        MallInfoData data = new MallInfoData();
        Field[] fields = param.getClass().getDeclaredFields();
        //获得变量名、变量值
        int num = 0;
        for (Field field : fields) {
            data.setKeyName(field.getName());
            field.setAccessible(true);
            try {
                data.setKeyValue((String) field.get(param));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            num = num + configMapper.updateMall(data.getKeyName(),data.getKeyValue());
        }
        return num;
    }

    @Override
    public List<MallInfoData> express() {

        List<MallInfoData> mallInfoData = configMapper.getMallInfo("express");
        return mallInfoData;
    }

    @Override
    public int updateExpress(ExpressParam param) {
        MallInfoData data = new MallInfoData();
        Field[] fields = param.getClass().getDeclaredFields();
        //获得变量名、变量值
        int num = 0;
        for (Field field : fields) {
            data.setKeyName(field.getName());
            field.setAccessible(true);
            try {
                data.setKeyValue((String) field.get(param));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            num = num + configMapper.updateMall(data.getKeyName(),data.getKeyValue());
        }
        return num;
    }

    @Override
    public List<MallInfoData> order(){
        List<MallInfoData> mallInfoData = configMapper.getMallInfo("order");
        return mallInfoData;
    }

    @Override
    public int updateOrder(OrderParam param) {
        MallInfoData data = new MallInfoData();
        Field[] fields = param.getClass().getDeclaredFields();
        //获得变量名、变量值
        int num = 0;
        for (Field field : fields) {
            data.setKeyName(field.getName());
            field.setAccessible(true);
            try {
                data.setKeyValue((String) field.get(param));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            num = num + configMapper.updateMall(data.getKeyName(),data.getKeyValue());
        }
        return num;
    }


    @Override
    public List<MallInfoData> wx() {
        List<MallInfoData> mallInfoData = configMapper.getMallInfo("wx");
        return mallInfoData;
    }



    @Override
    public int updateWx(WxParam param) {
        MallInfoData data = new MallInfoData();
        Field[] fields = param.getClass().getDeclaredFields();
        //获得变量名、变量值
        int num = 0;
        for (Field field : fields) {
            data.setKeyName(field.getName());
            field.setAccessible(true);
            try {
                data.setKeyValue((String) field.get(param));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            num = num + configMapper.updateMall(data.getKeyName(),data.getKeyValue());
        }
        return num;
    }


}
